import logging
import uuid

logger = logging.getLogger('server')


def get_summary(context):
    return ''.join(f'[{key}: {value}]' for key, value in context.items())


class ContextAdapter(logging.LoggerAdapter):
    def process(self, msg, kwargs):
        base_message = get_summary(self.extra)
        return '%s %s' % (base_message, msg), kwargs


class LoggingMixin:
    BASE_LOGGER = None

    @property
    def log_context(self):
        if not hasattr(self, '_logger_context'):
            self._logger_context = {'request_id': uuid.uuid4().hex}
        return self._logger_context

    @property
    def logger(self):
        if not hasattr(self, '_logger'):
            self._logger = ContextAdapter(logger, self.log_context)
        return self._logger
